﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Classes;
using System.Drawing;
using Silvermoon.Shapes;
using Silvermoon.Controls.Base;
using Silvermoon.Interfaces;
using Silvermoon.Utils;
using Silvermoon.Windows;

namespace Silvermoon.Controls
{

    public class Bubble : Control, IColor
    {
        public const int DefaultHeight = 96;
        const int cornerRadius = 32;


        private ShadowedBubbleShape background;
        private Color color = Color.WhiteSmoke;

        public Bubble()
            : base()
        {
            Height = DefaultHeight;
            TextColor = Color.Black;
            background = new ShadowedBubbleShape { Color = color, SelectedColor = PhoneColors.Blue };
            Shapes.AddFirst(background);
            Buttons = new BubbleButtonCollection(this);
        }

        public override bool CanFocus
        {
            get { return false; }
        }


        /// <summary>
        /// Gets or sets the location of the arrow.
        /// </summary>
        public Placement ArrowPlacement
        {
            get { return background.ArrowPlacement; }
            set
            {
                background.ArrowPlacement = value;
                Invalidate(InvalidationFlag.Appearance);
            }
        }

        /// <summary>
        /// Gets or sets the offset of the arrow relative to the center.
        /// </summary>
        public int ArrowOffset
        {
            get { return background.ArrowOffset; }
            set
            {
                value = ValidateArrowOffset(value);
                background.ArrowOffset = value;
                Invalidate(InvalidationFlag.Appearance);
            }
        }


        public override Color Color
        {
            get { return color; }
            set
            {
                if (color != value)
                {
                    color = value;
                    background.Color = value;
                    Invalidate(InvalidationFlag.Color);
                }
            }
        }

        public Color ClipColor
        {
            get { return background.SelectedColor; }
            set
            {
                if (background.SelectedColor != value)
                {
                    background.SelectedColor = value;
                    Invalidate(InvalidationFlag.Color);
                }
            }
        }

        public BubbleButtonCollection Buttons { get; private set; }

        private ControlCollection controls;

        /// <summary>
        /// If overloaded, returns a ControlCollection used to store child controls.
        /// </summary>
        /// <returns>ControlCollection, otherwise null</returns>
        protected internal override ControlCollection GetVisibleControls()
        {
            if (controls == null) controls = new ControlCollection(this);
            return controls;
        }

        protected override void OnLayout()
        {
            base.OnLayout();
            if (CheckFlag(InvalidationFlag.LayoutAppearance | InvalidationFlag.Size))
            {
                background.TileCount = Buttons.Count;
                background.Bounds = new Rectangle(0, 0, Width, Height);
                if (Buttons.Count > 0)
                {
                    int n = Buttons.Count;
                    int w = Width / n;

                    background.SelectedTile = -1;
                    int index = 0;
                    foreach (BubbleButton item in Buttons)
                    {
                        if (item.IsPressed)
                        {
                            background.SelectedTile = index;
                        }
                        item.Bounds = background.GetTileRect(index);

                        index++;
                    }
                }
            }
        }

        /// <summary>
        /// Renders the control with all shapes and child controls to the specified renderer.
        /// Occurs  after transformations are applied.
        /// </summary>
        /// <param name="renderer">Renderer which will render the control.</param>
        protected internal override void OnRender(Silvermoon.Core.Renderer renderer)
        {
            var shapes = Shapes;
            if (shapes != null) renderer.RenderShapes(shapes);

            var controls = this.controls;
            if (controls != null) renderer.RenderControls(controls);
        }

        private int ValidateArrowOffset(int value)
        {
            int max = Width / 2 - cornerRadius;
            if (value > max) return max;
            int min = -max;
            return value < min ? min : value;

        }

    }
}
